# Building Your First Amazon Virtual Private Cloud - 任务需求:创建一个基本的 Amazon Virtual Private Cloud (Amazon VPC),而无需使用 VPC 向导 ## 目标创建 Amazon Virtual Private Cloud (VPC) - 创建公有子网和私有子网 创建 Internet 网关 创建路由表并添加到 Internet 的路由 为您的 Web 服务器创建一个安全组,以仅允许 HTTP 流量流向您的 Web 服务器 为您的 MySQL RDS 实例创建一个安全组,以仅允许来自公有子网的 MySQL 流量 部署 Web 服务器和 MySQL RDS 实例 配置您的应用程序以连接到 MySQL RDS 实例 ## 实验室环境 ![](figures/experimental_framework.png) 以下列表详细介绍了图中的主要资源: - 一个 VPC,具有一个公有子网和两个跨两个可用区的私有子网。 一个 EC2 实例,充当公有子网中的 Web 服务器。 跨两个私有子网托管的 MySQL RDS 实例。 允许来自外部用户的入站流量的 Internet 网关。 ## 实验手册 ### 任务 1:创建 VPC 1. 在 AWS Management Console 顶部的搜索栏中,搜索并选择专有网络. 2. 选择```创建 VPC```. 3. 在 Create VPC (创建 VPC) 页面上: - 对于 Resources to create (要创建的资源),选择 VPC Only (仅限 VPC)。 对于 Name tag - optional (名称标签 - 可选),输入```我的 VPC```. 对于 IPv4 CIDR,输入```10.0.0.0/16```. 5. 选择创建 ```VPC```. ### 任务 2:创建公有子网 6. 在左侧导航窗格中的 Virtual private cloud 下,选择 Subnets (子网)。 7. 选择```创建子网```. 8. 在 Create subnet (创建子网) 页面上: - 对于 VPC ID,从下拉菜单中选择 My VPC。 对于 Subnet name (子网名称),输入公共 1. 对于 Availability Zone(可用区),选择列表中的第一个可用区。 对于 IPv4 subnet CIDR block(IPv4 子网 CIDR 块),输入10.0.1.0/24. 9. 选择```创建子网```. 10. 选择 Public 1(公共 1)。 11. 在 Actions (操作) 菜单中,选择 Edit subnet settings (编辑子网设置)。 12. 在 Edit subnet settings (编辑子网设置) 页面上: 13. 选择```保存``` ### 任务 3:创建 Internet 网关 14. 在左侧导航窗格中的 Virtual private cloud 下,选择 Internet gateways。 15. 选择创建``` Internet 网关```. 16. 在 Create internet gateway (创建互联网网关) 页面上: - 对于 Name tag (名称标签),输入```我的 IG```. 17. 选择创建 Internet 网关. 18. 在 Actions (操作) 菜单中,选择 Attach to VPC (附加到 VPC)。 19. 在 Available VPCs (可用 VPC) 搜索框中,选择 My VPC(我的 VPC)。 20. 选择附加 Internet 网关. ### 任务 4:创建路由表、添加路由并关联公有子网 21. 在左侧导航窗格中的 Virtual private cloud 下,选择``` Route tables ```(路由表)。 22. 选择创建路由表. 23. 在 Create route table (创建路由表) 页面上: - 对于 Name - optional (名称 - 可选),输入公有路由表. 对于 VPC,从下拉菜单中选择 My VPC。 24. 选择```创建路由表```. 25. 选择页面下半部分的 Routes (路由) 选项卡。 26. 选择```编辑路由```. 27. 选择```添加路由```. 28. 在 Edit routes (编辑路由) 页面上: - 对于 Destination (目标),输入0.0.0.0/0. 对于 Target,选择 Internet 网关 在下拉列表中选择,然后选择 My IG Internet Gateway ID。 29. 选择保存更改. 30. 选择 ```Subnet associations``` (子网关联) 选项卡。 31. 在 Explicit subnet associations (显式子网关联) 部分中,选择编辑子网关联. 32. 选择 Public 1(公共 1)。 33. 选择```保存关联```. ### 任务 5:为您的 Web 服务器创建安全组 34. 在左侧导航窗格中的 Security (安全) 下,选择```安全组```。 35. 选择```创建安全组```. 36. 在 Create security group (创建安全组) 页面上: - 对于 Security group name (安全组名称),输入网页服务器. 对于 Description (描述),输入我的 Web 服务器安全组. 对于 VPC,从下拉菜单中选择 My VPC。 37. 在 Inbound rules (入站规则) 部分中: - 选择添加规则. 对于 Type (类型),从下拉菜单中选择 HTTP。 对于 Source (源),从下拉菜单中选择 Anywhere-Ipv4 。 38. 在屏幕底部,选择创建安全组. ### 任务 6:在公有子网中启动 Web 服务器 39. 在 AWS Management Console 顶部的搜索栏中,搜索并选择EC2. 40. 在左侧导航窗格中的 Instances (实例) 下,选择 Instances (实例)。 41. 选择启动实例. 42. 在 Launch an instance (启动实例) 页面中: - 对于 Name and tags (名称和标签),输入网页服务器. - 对于 Application and OS Images (Amazon Machine Image) (应用程序和操作系统映像 (Amazon 系统映像),选择 Amazon Linux 2 AMI。 - 对于 Instance type (实例类型),选择 t3.micro。 - 对于 Key pair (login) section (密钥对 (登录) 部分),选择 Proceed without a key pair (在没有密钥对的情况下继续)。 - 对于 Network settings (网络设置) 部分,选择编辑. - 对于 VPC,选择 My VPC (我的 VPC)。 注意:Public 1 在 subnet 部分下自动填充。 - 对于 Firewall (security groups) (防火墙 (安全组),选择 Select existing security group (选择现有安全组)。 - 对于 Common security groups (常见安全组),选择 Web server (Web 服务器)。 向下滚动并展开 高级详细信息 将此脚本复制并粘贴到 User data - optional 文本框中: ``` #!/bin/bash -ex yum -y update yum -y install httpd php mysql php-mysql chkconfig httpd on service httpd start cd /var/www/html TOKEN=$(curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600") && AWS_REGION=$(curl -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/dynamic/instance-identity/document | grep -oP '(?<="region" : ").*?(?=")') && wget https://$AWS_REGION-tcprod.s3.$AWS_REGION.amazonaws.com/courses/spl-13/v4.2.33.prod-90e4142c/scripts/app.tgz tar xvfz app.tgz chown apache:root /var/www/html/rds.conf.php ``` 43. 选择启动实例. 44. 选择查看所有实例. 这将带您进入 Instances (实例) 窗口,您可以在其中查看您的 Web 服务器详细信息。 45. 等待 Web 服务器完全启动。它应显示以下内容: ``` 实例状态: 运行. 状态检查: 3/3 检查通过 ``` 您可以选择刷新图标来刷新您的实例状态。 46. 选择 Web 服务器实例。 47. 复制编辑:将实例的 Public IPv4 address address(公有 IPv4 地址地址)复制到剪贴板。 48. 打开一个新的 Web 浏览器选项卡,然后将 IP 地址粘贴到浏览器中。 按 Enter 键转到网页。 谨慎:确保 URL 以 http:// 开头,而不是 https://。 ![alt text](figures/display_program.png) ### 任务 7:为您的 MySQL 服务器创建私有子网 1. 在 AWS Management Console 顶部的搜索栏中,搜索并选择专有网络. 1. 在左侧导航窗格中的 Virtual private cloud 下,选择 ```Subnets ```(子网)。 2. 选择```创建子网```. 3. 在 Create subnet (创建子网) 页面上: - 对于 VPC,从下拉菜单中选择 My VPC。 对于 Subnet name (子网名称),输入私人 1. 对于 Availability Zone(可用区),选择列表中的第一个可用区。 对于 IPv4 subnet CIDR block(IPv4 子网 CIDR 块),输入```10.0.2.0/24```. 53. 选择创建子网. 54. 在 Create subnet (创建子网) 页面上: - 对于 VPC,从下拉菜单中选择 My VPC。 对于 Subnet name (子网名称),输入私人 1. 对于 Availability Zone(可用区),选择列表中的第一个可用区。 对于 IPv4 subnet CIDR block(IPv4 子网 CIDR 块),输入10.0.2.0/24. 选择添加新子网. 55. 在 Create subnet (创建子网) 页面上: - 对于 VPC,从下拉菜单中选择 My VPC。 对于 Subnet name (子网名称),输入私人 2. 对于 Availability Zone(可用区),选择列表中的第二个可用区。 对于 IPv4 subnet CIDR block(IPv4 子网 CIDR 块),输入10.0.3.0/24. 选择创建子网.选择添加新子网. ### 任务 8:为数据库服务器创建安全组 56. 在左侧导航窗格中,选择 Security Groups (安全组)。 57. 复制编辑:复制 Web 服务器安全组的 Security group ID 值,并将其粘贴到文本编辑器中。 58. 选择创建安全组. 59. 在 Create security group (创建安全组) 页面上: - 对于 Security group name (安全组名称),输入数据库. 对于 Description (描述),输入我的数据库安全组. 对于 VPC,从下拉菜单中选择 My VPC。 60. 在 Inbound rules (入站规则) 部分中: - 选择添加规则. 对于 Type(类型),从下拉菜单中选择 MySQL/Aurora。 警告:确保选择 MYSQL/Aurora 而不是 MSSQL。 对于 Source (源),选择 Custom (自定义),然后将您复制到文本编辑器的 Web 服务器安全组 ID 粘贴。 61. 在屏幕底部,选择创建安全组. ### 任务 9:创建数据库子网组 62. 在 AWS Management Console 顶部的搜索栏中,搜索并选择云数据库 RDS. 63. 在左侧导航窗格中,选择 Subnet groups (子网组)。 64. 选择```创建 DB 子网组```. 65. 在 Create DB subnet group (创建数据库子网组) 页面的 Subnet group details (子网组详细信息) 部分中: - 对于 Name (名称),输入```我的子网组```. 对于 Description (描述),输入```我的子网组```. 对于 VPC,从下拉菜单中选择 ```My VPC```。 66. 在 Add subnets (添加子网) 部分中: - 对于 Availability zones (可用区),选择列表中的第一个和第二个 Availability Zones (可用区)。 对于 Subnets (子网),选择: ```10.0.2.0/24``` ```10.0.3.0/24``` 在屏幕底部,选择```创造```. ### 任务 10:创建 Amazon RDS 数据库 67. 在左侧导航窗格中,选择 Databases (数据库)。 68. 选择创建数据库. 69. 在 Create database 页面的 Engine options 部分: - 对于 Engine options (引擎选项),选择 MySQL 70. 在 Templates 部分中,选择 Dev/Test。 71. 在 Settings(设置)部分中,进行以下配置: - DB instance identifier(数据库实例标识符):myDB Master username(主用户名):admin 对于 Credentials management(凭证管理),选择 Self managed(自管理)选项 Master password(主密码):lab-password Confirm password(确认密码):lab-password 72. 在 DB instance class(数据库实例类)部分中,进行以下配置: - DB instance class(数据库实例类):Burstable classes(可突增类) 选择 db.t3.micro 73. 在 Storage(存储)部分,展开 Storage autoscaling(存储自动扩展),然后取消选中 Enable storage autoscaling(启用存储自动扩展) 74. 在 Connectivity(连接)部分中,进行以下配置: - Virtual Private Cloud (VPC):My VPC Public access(公有访问): No(否) Existing VPC security groups(现有 VPC 安全组): 添加 Database 安全组 移除 default 安全组 75. 在 Monitoring(监控)部分,取消选中 启用增强监控 76. 在 Additional configuration(其他配置)部分(位于底部附近)中,选择 Additional configuration(其他配置),然后配置: - Initial database name(初始数据库名称):myDB 取消选中 Enable automated backups(启用自动备份)。此操作将关闭备份,这样可以更快地为您的实验启动数据库。 取消选中 Enable auto minor version upgrade(允许自动次要版本升级 77. 在屏幕底部,选择 Create database(创建数据库) 78. 如果您收到一个弹出窗口,显示 Suggested add-ons for mydb(mydb 的建议附加组件),选择 Close(关闭) 79. 每 60 秒选择刷新 一次,直到实例的状态为 available(可用)。 ### 任务 11:将通讯簿应用程序连接到数据库 **获取 MySQL 数据库终端节点** 80. 选择您的 mydb 实例。 81. 在 Connectivity & security(连接和安全)部分中,将 Endpoint(终端节点)复制到剪贴板。 - 您的 RDS 终端节点应类似于: mydb.ciljcs3yv1rb.us-west-2.rds.amazonaws.com **连接到数据库** 82. 返回显示 Web 服务器的浏览器选项卡,然后进行以下配置: Endpoint(终端节点):粘贴您的 MySQL 终端节点 Database(数据库):myDB Username(用户名):admin Password(密码):lab-password 选择 Submit(提交) 预期输出: ![alt text](figures/Expected_output.png)